/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			session.cc
  \date			Feb 2012
  \author		TNick

  \brief		Contains the implementation of Session class


*//*


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read COPYING and README files in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */


#include	<pgscript.h>
#include	<pgscript_internal.h>

#include	<pgsave/ipgsave.h>
#include	<uservar.h>

#include	"session.h"


/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */

using namespace PgScr;

/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

Session *		Session::def_ss = NULL;


/*  DATA    ================================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
Session::Session	( void )
	: def_md_( QString( SESSION_DEF_MODULE ), this )
{
	module_list_.clear();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
Session::~Session	( void )
{
	module_list_.clear();
	core_list_.clear();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
Module *			Session::module			(QString & s_name)
{
	for ( int i = 0; i < module_list_.count( ); i++ )
	{
		if ( module_list_.at( i )->name() == s_name )
			return module_list_.at( i );
	}
	return NULL;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
Session *			Session::load			(
		QString s_name, PgSave::IPgSave & targ_i )
{
	int					i;
	bool				b_tmp;
	QString				s_name_loc;

	targ_i.setPath( s_name );

	/* check session marker */
	Q_ASSERT( targ_i.getStr( SESSION_NAME, &b_tmp ) == s_name );
	Q_ASSERT( b_tmp == true );
	targ_i.set( SESSION_NAME, s_name );

	Session *	p_ses = new Session();


	/* (default module */
	targ_i.setPath( DEF_MOD_GROUP );

	p_ses->def_md_.loadFull( targ_i );

	targ_i.cdUp();
	/* default module */


	/* modules */
	targ_i.setPath( MODULES_GROUP );

	i = 0;
	while ( i < MAX_COMPONENTS_COUNT )
	{
		s_name_loc.setNum( i );
		if ( ! targ_i.exists( s_name_loc + "/" + MOD_NAME_GROUP ))
			break;

		Module::loadFull( s_name_loc, p_ses, targ_i ) ;

		i++;
	}

	targ_i.cdUp();
	/* modules */


	/* core modules list */
	targ_i.setPath( CORE_M_GROUP );

	i = 0;
	while ( i < MAX_COMPONENTS_COUNT )
	{
		s_name_loc.setNum( i );
		s_name = targ_i.getStr( s_name_loc, &b_tmp );
		if ( ! targ_i.exists( MOD_NAME_GROUP ))
			break;
		for ( int j; j < p_ses->module_list_.count(); j++ )
		{
			if ( s_name == p_ses->module_list_.at( j )->name() )
			{
				p_ses->core_list_.append( p_ses->module_list_.at( j ) );
				break;
			}
		}
		i++;
	}

	targ_i.cdUp();
	/* core modules list */

	targ_i.cdUp();

	return p_ses;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void				Session::save (
		QString s_name, PgSave::IPgSave & targ_i )
{
	int i;


	targ_i.setPath( s_name );


	/* create session marker */
	targ_i.set( SESSION_NAME, s_name );




	/* (default module */
	targ_i.setPath( DEF_MOD_GROUP );

	def_md_.save( targ_i );

	targ_i.cdUp();
	/* default module */




	/* modules */
	targ_i.setPath( MODULES_GROUP );

	for ( i = 0; i < module_list_.count(); i++ )
	{
		module_list_.at( i )->save( targ_i );
	}

	targ_i.cdUp();
	/* modules */




	/* core modules list */
	targ_i.setPath( CORE_M_GROUP );

	for ( i = 0; i < core_list_.count(); i++ )
	{
		targ_i.set( QString::number( i ),
					core_list_.at( i )->name() );
	}

	targ_i.cdUp();
	/* core modules list */



	targ_i.cdUp();
}
/* ========================================================================= */







#ifdef	PGSCRIPT_TESTS

#include	<pgsave/pgsqs.h>

/* ------------------------------------------------------------------------- */
void				PgScr::Session_Tests					(
		int & performed, int & succeded )
{
	QString		Outcm;
	performed = 0;
	succeded = 0;

	/* save interface to use */
	PgSave::PgsQs s_i;
	QString s_pth_to_save =  "/test/in/session" ;
	s_i.setPath( s_pth_to_save );


	/* create a list of sessions */
	QList<Session*>	sl;
	int		i_s_cnt = 10;
	int		i_m_cnt = 10;
	int		i_v_cnt = 10;
	int		i;
	int		j;
	int		k;
	Module *	m;
	UserVar *	var_1;
	for ( i = 0; i < i_s_cnt; i++ )
	{
		Session * s = new Session();
		sl.append( s );

		for ( j = 0; j < i_m_cnt; j++ )
		{
			/* create a number of modules in each session */
			m = new Module(
						QString( "test_module_s_%1_m_%2_" )
						.arg( i )
						.arg( j ),
						s);
			for ( k = 0; k < i_v_cnt; k++ )
			{
				var_1 = new UserVar(
							QString( "test_module_s_%1_m_%2_v_%3_" )
							.arg( i )
							.arg( j )
							.arg( k ),
							m );
			}
		}
		for ( k = 0; k < i_v_cnt; k++ )
		{
			var_1 = new UserVar(
						QString( "test_module_s_%1_m_def_v_%2_" )
						.arg( i )
						.arg( k ),
						s );
		}

	}
	var_1 = NULL; var_1++;

	/* save the list of sessions */
	saveSessionList( "ss_list", sl, s_i );


	/* destroy the list of sessions */
	for ( i = 0; i < i_s_cnt; i++ )
	{
		delete sl.at( i );
	}
	sl.clear();


	/* reload the list of sessions */
	loadSessionList( sl, s_i );

	if ( s_pth_to_save == s_i.path() )	TEST_OK
			else	TEST_FAIL
			qDebug() << "Path preserved across calls..." << Outcm;
	performed++;

}
/* ========================================================================= */
#endif

/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
